package de.hdu.pms.dao;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import de.hdu.pms.model.Pausen;

public class PausenDao extends HibernateDaoSupport {

	public static Logger Log = Logger.getLogger(PausenDao.class);

	public static void setLog(Logger log) {
		Log = log;
	}

	/**
	 * returns all entries from the table Pausen with a special id
	 * 
	 * @return a list of Pausen
	 * @see Pausen
	 */

	public Pausen findById(int pausen_id) {
		HibernateTemplate template = getHibernateTemplate();
		return (Pausen) template.get(Pausen.class, pausen_id);
	}

	/**
	 * Lists all entries from the table Pausen
	 * 
	 * @returns als list of Pausen
	 * @see Pausen
	 * @author BBV
	 */

	public List<Pausen> findAll() {
		HibernateTemplate template = getHibernateTemplate();
		List<Pausen> results = template.loadAll(Pausen.class);
		return results;
	}

	/**
	 * Saves a entry for the table Pausen
	 * 
	 * @return pausen
	 * @param pausen
	 * @author BBV
	 */

	public Pausen save(Pausen pausen) {
		HibernateTemplate template = getHibernateTemplate();
		template.saveOrUpdate(pausen);
		return pausen;
	}

	/**
	 * deletes a entry in the table pausen
	 * 
	 * @param pausen
	 * @author BBV
	 */
	public void delete(Pausen pausen) {
		HibernateTemplate template = getHibernateTemplate();
		template.delete(pausen);
	}

	/**
	 * Function to find not ended breaks If breaks are empty return = null else
	 * it returns the found breaks
	 * 
	 * @author BBV
	 * @param atag_id
	 * @return Pausen
	 */

	@SuppressWarnings("unchecked")
	public Pausen findNichtBeendet(Integer atag_id) {
		HibernateTemplate template = getHibernateTemplate();
		List<Pausen> pausen = template
				.find("from Pausen p WHERE p.arbeitstag.id = " + atag_id
						+ " AND p.pause_stop IS NULL");
		if (pausen.isEmpty()) {
			return null;
		} else {
			Integer pausen_id = pausen.get(0).getId();
			return (Pausen) template.get(Pausen.class, pausen_id);
		}
	}

	/**
	 * Function to list the breaks to a working day
	 * 
	 * @author BBV
	 * @param atag_id
	 * @return results
	 */

	@SuppressWarnings("unchecked")
	public List<Pausen> findByAtag(Integer atag_id) {
		HibernateTemplate template = getHibernateTemplate();
		List<Pausen> results = template
				.find("from Pausen p WHERE p.arbeitstag.id = " + atag_id);
		return results;
	}

	/**
	 * Function to list the not ended breaks
	 * 
	 * @param datum
	 * @return results
	 */

	@SuppressWarnings("unchecked")
	public List<Pausen> findCron(Date datum) {
		HibernateTemplate template = getHibernateTemplate();
		SimpleDateFormat formatDatum = new SimpleDateFormat("yyyy-MM-dd");
		List<Pausen> results = template
				.find("from Pausen p where p.arbeitstag.atag_datum <> '"
						+ formatDatum.format(datum)
						+ "' and p.pause_stop is null");
		return results;
	}

	/**
	 * 
	 * Function to list breaks in a time span
	 * 
	 * @param start
	 * @param end
	 * @param atag_id
	 * @return
	 */

	@SuppressWarnings("unchecked")
	public List<Pausen> findBySpan(Date start, Date end, Integer atag_id) {
		HibernateTemplate template = getHibernateTemplate();
		SimpleDateFormat formatDatum = new SimpleDateFormat("HH:mm:ss");
		List<Pausen> results = template.find("from Pausen p where p.pause_start >= '" + formatDatum.format(start) + "' and p.pause_stop <= '" + formatDatum.format(end) + "' and p.arbeitstag.id = " + atag_id);
		return results;
	}
}
